﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Framework.DataAccess;

public partial class InventoryList : PMSBase
{
    //private SqlCodeConfig codeConfig;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TableName = "IC_Goods";
        this.Key = "GoodsID";
        if (!IsPostBack)
        {
            pnlGrid.Visible = true;
            StringBuilder sql = new StringBuilder(string.Format("SELECT T.*,A.SUBJECT AS CategoryName FROM {0} T LEFT JOIN " + GoodsCategoryAccess.TABLE_NAME + " A ON A.NODEID=T.CategoryID where 1=1 ", this.TableName));
            sql.Append(" ORDER BY CREATETIME DESC");
            FillGirdView(0, sql.ToString());
            InitLoadData();
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }
    private void InitLoadData()
    {
        GoodsCategoryAccess goodsCategoryAccess = new GoodsCategoryAccess(base.Connection);
        DataSet ds = goodsCategoryAccess.LoadTreeData(true, false);
        DataToTree(ds, treeView, null);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        StringBuilder sql = new StringBuilder(string.Format("SELECT T.*,A.SUBJECT AS CategoryName FROM {0} T LEFT JOIN " + GoodsCategoryAccess.TABLE_NAME + " A ON A.NODEID=T.CategoryID where 1=1 ",this.TableName));
        if (!string.IsNullOrEmpty(S_Subject.Text))
        {
            sql.Append(" AND T.Subject like '%").Append(S_Subject.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_GoodsCode.Text))
        {
            sql.Append(" AND T.GoodsCode like '%").Append(S_GoodsCode.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_CategoryID.Text) && treeView.SelectedNode!=null)
        {
            sql.Append(" AND T.CategoryID = '").Append(treeView.SelectedNode.Value).Append("'");
        }
        sql.Append(" ORDER BY T.CREATETIME DESC");
        FillGirdView(0, sql.ToString());
    }
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = false;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((HyperLink)e.Row.Cells[1].Controls[0]).NavigateUrl = "GoodDetail.aspx?GoodsID=" + ((HiddenField)e.Row.Cells[0].Controls[1]).Value;
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }

    /// <summary>
    /// 数据集转换到节点
    /// </summary>
    /// <param name="dataSet"></param>
    /// <param name="treeView"></param>
    /// <param name="node"></param>
    private void DataToTree(DataSet dataSet, TreeView treeView, TreeNode node)
    {
        String filter;
        if (node != null)
            filter = "ParentID = '" + node.Value + "'";
        else
            filter = "ParentID = '0'";

        for (int i = dataSet.Tables[0].Select(filter, "OrderIndex").Length - 1; i >= 0; i--)
        {
            DataRow row = dataSet.Tables[0].Select(filter, "OrderIndex")[i];
            TreeNode child = new TreeNode();
            child.Value = row["NODEID"].ToString();
            child.Text = row["SUBJECT"].ToString();
            //child.ToolTip = row["Alias"].ToString();
            //child.NavigateUrl = "#";

            if (node != null)
            {
                node.ChildNodes.Add(child);
                if (node.Depth < 1)
                {
                    node.Expand();
                }
                else
                {
                    node.Collapse();
                }
            }
            else
                treeView.Nodes.Add(child);
            DataToTree(dataSet, treeView, child);
        }
    }


    /// <summary>
    /// 返回当前节点的上一个节点
    /// </summary>
    public TreeNode FirstNode(TreeNode node)
    {
        if (node.ChildNodes.Count > 0)
        {
            return node.ChildNodes[0];
        }
        else
        {
            return null;
        }
    }


    /// <summary>
    /// 返回当前节点的下一个节点
    /// </summary>
    public TreeNode NextNode(TreeNode node)
    {
        if (node.Parent != null)
        {
            for (int i = 0; i < node.Parent.ChildNodes.Count; i++)
            {
                if (node.Parent.ChildNodes[i] == node)
                {
                    if (i < node.Parent.ChildNodes.Count - 1)
                    {
                        return node.Parent.ChildNodes[i + 1];
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            //永远执行不到
            return null;
        }
        else
        {
            return null;
        }
    }


    /// <summary>
    /// 在树中查找节点
    /// </summary>
    protected TreeNode DepthFirstSearch(TreeNode node, string nodeID)
    {
        //do something
        TreeNode result = null;
        if (node.Value == nodeID)
        {
            return node;
        }

        //TreeNode child = child.FirstNode;
        TreeNode child = FirstNode(node);
        while (child != null)
        {
            result = DepthFirstSearch(child, nodeID);
            if (result != null && result.Value == nodeID)
            {
                result.Selected = true;
                return result;
            }
            //child = child.NextNode;
            child = NextNode(child);
        }
        return result;
    }

    protected void treeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (treeView.SelectedNode == null)
        {
            PopupControlExtender1.Cancel();
        }
        else
        {
            PopupControlExtender1.Commit(treeView.SelectedNode.Text);
        }
    }

}
